<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/7/25
 * Time: 16:08
 *
 */
namespace app\api\controller;
use think\Db;
class Instructor extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        header("Access-Control-Allow-Origin:*");
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 2111){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }
    }

    //自媒体首页
    public function index(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $res = Db::table('up_instructor')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.media_user_id','left')
            ->where('is_publish', 1)
            ->order('a.media_time desc')
            ->field('uuu.user_username,uuu.user_username,a.*')
            ->limit($limit*$num,$num)
            ->select();
//        foreach ($res as $key=>&$vo){
//            if($vo['is_admin']==1){//前段用户
//                $vo['user_username'] = Db::table('up_u_user')->where('user_id',$vo['media_user_id'])->value('user_username');
//            }else{
//                $vo['user_username'] = Db::table('up_admin')->where('id',$vo['media_user_id'])->value('username');
//            }
//        }
        return  json(array('status'=>200,'msg'=>$res));
    }

    //发布自媒体
    public function addMedia(){
        extract(input());
        $is_show_user  = input('is_show_user'); // 是否匿名    1 不匿名   2 匿名
        $is_publish  = input('is_publish'); // 是否发布    1 发布    2 草稿箱
        $title  = input('param.title');
        $content  = input('param.content');
        $title  = $this->sensitive($title);
        $content  = $this->sensitive($content);
        $media_url  = input('param.img');
        $data['media_title'] = $title;
        $data['media_content'] = $content;
        $data['media_url'] = $media_url;
        $data['media_user_id'] = $user_id;
        $data['media_time'] = date('Y-m-d H:i:s',time());
        $data['media_update_time'] = date('Y-m-d H:i:s',time());
        $data['is_show_user'] = $is_show_user;
        $data['is_publish'] = $is_publish;
        $img = explode(',',$media_url);
        $data['media_url_thumb'] =$img[0];
        $res =  Db::table('up_instructor')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }
    //详情
    public function mediaDetail(){
        extract(input());
        $id = input('param.id');
        $page = input('param.page')?input('param.page'):1;
        $num1 = 10;
        $limit = $page-1;
        $where['media_id'] = $id;
        $res = Db::table('up_instructor')->alias('a')
            ->where($where)
            ->field('a.*')
            ->find();
        if(!$res){
            return json(['status' => 0, 'msg' => '数据不存在']);
        }
        //查询是否收藏这个资讯
        $whereMap4['collection_f_id'] = $id;
        $whereMap4['collection_type'] = 7;
        $whereMap4['collection_user_id'] = $user_id;
        $collection = Db::table('up_collection')->where($whereMap4)->find();
        if($collection['collection_id']!=''){
            $res['is_collection'] = 1;
        }else{
            $res['is_collection'] = 2;
        }
//        if($res['is_admin']==1){//前段用户
        $res['user_username'] = Db::table('up_u_user')->where('user_id',$res['media_user_id'])->value('user_username');
        $res['portrait'] = Db::table('up_u_user')->where('user_id',$res['media_user_id'])->value('user_image');

//        }else{
//            $res['user_username'] = Db::table('up_admin')->where('id',$res['media_user_id'])->value('username');
//            $res['portrait'] = Db::table('up_admin')->where('id',$res['media_user_id'])->value('portrait');
//
//        }
        //统计多少评论
        $where1['f_id'] = $id;
        $where1['type'] = 10;
        $num = Db::table('up_comment')->alias('a')
            ->where($where1)
            ->count();
        $res['share_num'] = $num;
        //查询评论
        $where2['f_id'] =$id;
        $where2['type'] =10;
        //查询评论数据
        $res1 = Db::table('up_comment')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where($where2)
            ->field('uuu.user_username,uuu.user_image,a.comment,a.comment_id,a.time,a.user_id,a.is_show_user')
            ->limit($limit*$num1,$num1)
            ->select();
        foreach ($res1 as $key=>&$vo) {
            //评论有多少回复
            $count = Db::table('up_comment_reply')->where('f_id', $vo['comment_id'])->count();
            $vo['count'] = $count;
            //评论有多少点赞
            $map['give_f_id'] = $vo['comment_id'];
            $map['give_type'] = 17;
            $count1 = Db::table('up_comment_give')->where($map)->count();
            $vo['count1'] = $count1;

            $map1['give_type'] = 17;
            $map1['give_f_id'] = $vo['comment_id'];
            $map1['give_user_id'] = $user_id;
            $info = Db::table('up_comment_give')->where($map1)->find();
            if ($info['give_id'] != '') {//点赞过
                $vo['is_dz'] = 1;
            } else {
                $vo['is_dz'] = 2;
            }
        }
        //查询是否收藏这个资讯

        $res['data']=$res1;
        return  json(array('status'=>200,'msg'=>$res));
    }
    //收藏
    public function collection(){
        // type  1评论回复点赞   2评论回复点赞
        extract(input());
        $id = input('param.id');
        $data['collection_f_id'] = $id;
        $data['collection_user_id'] = $user_id;
        $data['collection_type'] = 7;
        $res1 = Db::table('up_collection')->where($data)->find();
        if($res1['collection_id']!=''){
            $res= Db::table('up_collection')->where($data)->delete();
        }else{
            $data['collection_type']=7;
            $res= Db::table('up_collection')->insertGetId($data);
        }
        if ($res) {
            return json(array('status' => 200, 'msg' => "收藏成功"));
        } else {
            return json(array('status' => 200, 'msg' => "收藏失败"));
        }
    }
    //评论添加
    public function comment(){
        extract(input());
        $is_show_user = input('is_show_user');
        $content = input('param.content');
        $content = $this->sensitive($content);
        $video_id = input('param.media_id');
        $data['f_id'] = $video_id;
        $data['comment'] = $content;
        $data['time'] = date('Y-m-d H:i:s',time());
        $data['user_id'] = $user_id;
        $data['type'] = 10;
        $data['is_show_user'] = $is_show_user;
        //todo
        $data['f_user_id'] = input('param.other_user');
        $res = Db::table('up_comment')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"发布成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"发布失败"));
        }
    }
    //评论点赞
    public function Dz(){
        // type  1评论回复点赞   2评论回复点赞
        extract(input());
        $type = input('param.type');
        if($type==1) {
            $data['give_user_id'] = $user_id;
            $data['give_time'] = date('Y-m-d H:i:s', time());
            $data['give_type'] = 17;
            $data['give_f_id'] = input('param.id');
            $data['give_f_f_id'] = input('param.pl_id');
            $data['give_by_user_id'] = input('param.other_user');
            $res = Db::table('up_comment_give')->insertGetId($data);
            if ($res) {
                return json(array('status' => 200, 'msg' => "点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "点赞失败"));
            }
        }elseif ($type==2){
            $data['give_user_id'] = $user_id;
            $data['give_type'] =17;
            $data['give_f_id'] = input('param.id');
            $res = Db::table('up_comment_give')->where($data)->delete();
            if ($res) {
                return json(array('status' => 200, 'msg' => "取消点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "取消点赞失败"));
            }
        }
    }

    //评论回复点赞
    public function pingDz()
    {
        // type  1评论回复点赞   2评论回复取消点赞
        extract(input());
        $type = input('param.type');
        if ($type == 1) {
            $data['give_user_id'] = $user_id;
            $data['give_time'] = date('Y-m-d H:i:s', time());
            $data['give_type'] = 18;
            $data['give_f_id'] = input('param.id');
            $data['give_f_f_id'] = input('param.pl_id');
            $data['give_by_user_id'] = input('param.other_user');
            $res = Db::table('up_comment_give')->insertGetId($data);
            if ($res) {
                return json(array('status' => 200, 'msg' => "点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "点赞失败"));
            }
        } elseif ($type == 2) {
            $data['give_user_id'] = $user_id;
            $data['give_type'] = 18;
            $data['give_f_id'] = input('param.id');
            $res = Db::table('up_comment_give')->where($data)->delete();
            if ($res) {
                return json(array('status' => 200, 'msg' => "取消点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "取消点赞失败"));
            }
        }
    }

    //评论回复添加
    public function answer(){
        extract(input());
        $is_show_user = input('is_show_user');
        $content = input('param.content');
        $content = $this->sensitive($content);
        $comment_id = input('param.id');
        $zx_id = input('zx_id');
        $data['f_id'] = $comment_id;
        $data['f_f_id'] = $zx_id;
        $data['comment_reply_content'] = $content;
        $data['comment_time'] = date('Y-m-d H:i:s',time());
        $data['user_id'] = $user_id;
        $data['by_user_id'] = input('param.other_user');
        $data['is_show_user'] = $is_show_user;
        $res = Db::table('up_comment_reply')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"发布成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"发布失败"));
        }
    }

    //资讯评论回复
    public function informationAnswer(){
        extract(input());
        $comment_id = input('param.information_id');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        //查询详情
        $where['comment_id'] = $comment_id;
        $res = Db::table('up_comment')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where($where)
            ->field('uuu.user_username,uuu.user_image,a.comment,a.comment_id,a.time,a.user_id,a.is_show_user')
            ->find();
        //多少点赞
        $whereMap['give_type'] = 17;
        $whereMap['give_f_id'] = $comment_id;
        $count2 = Db::table('up_comment_give')->where($whereMap)->count();
        $res['count2'] = $count2;
        //自己是否点赞
        $whereMap1['give_type'] = 17;
        $whereMap1['give_f_id'] = $comment_id;
        $whereMap1['give_user_id'] = $user_id;
        $count3 = Db::table('up_comment_give')->where($whereMap1)->find();
        if($count3['give_id']!=''){
            $res['dz'] = 1;
        }else{
            $res['dz'] = 2;
        }
        //查询回复数据
        $map2['a.f_id'] = $comment_id;
        $info = Db::table('up_comment_reply')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->where($map2)
            ->limit($num*$limit,$num)
            ->select();
        foreach ($info as $key=>&$vo){
            //评论回复有多少点赞
            $map['give_f_id'] = $vo['comment_reply_id'];
            $map['give_type'] = 18;
            $count1 = Db::table('up_comment_give')->where($map)->count();
            $vo['count1'] = $count1;
            //点赞
            $map1['give_type'] = 18;
            $map1['give_f_id'] = $vo['comment_reply_id'];
            $map1['give_user_id'] = $user_id;
            $info1 = Db::table('up_comment_give')->where($map1)->find();
            if($info1['give_id']!=''){//点赞过
                $vo['is_dz'] = 1;
            }else{
                $vo['is_dz'] = 2;
            }
        }
        //多少回复
        $num = Db::table('up_comment_reply')
            ->where('f_id',$comment_id)
            ->count();
        $res['num'] = $num;
        $res['info'] = $info;
        return  json(array('status'=>200,'msg'=>$res));
    }
}